﻿using DevExpress.XtraEditors;
using MM.Common.Base;
using MM.Common.DBBase;
using MM.Common.Setup;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmRoleSetup : FrmSetupWithItem
    {
        public FrmRoleSetup()
        {
            InitializeComponent();
            base.gvItem = this.gvEmp;
            base.gvList = this.gvRole;
        }
        protected override void afterInsert()
        {
            this.txtCode.Text = "";
            this.txtName.Text = "";
            this.meDescribe.Text = "";
            base.dtItem.Rows.Clear();
        }

        protected override void changeMode(bool edit)
        {
            base.changeMode(edit);
            base.pgBase.Enabled = true;
            this.txtName.Properties.ReadOnly = !edit;
            this.meDescribe.Properties.ReadOnly = !edit;
            this.txtCode.Properties.ReadOnly = true;
            this.gcGrantFlag.OptionsColumn.AllowEdit = edit;
            if (!SystemInfo.OperIsAdmin)
            {
                base.nbiNew.Visible = false;
                base.nbiDelete.Visible = false;
            }
        }

        protected override void deleteRecord()
        {
            if ((this.gvRole.FocusedRowHandle >= 0) && (XtraMessageBox.Show("是否真的删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
            {
                DataRow focusedDataRow = base.gvList.GetFocusedDataRow();
                string paramValue = focusedDataRow["ROLE_CODE"].ToString();
                string[] commandtexts = new string[2];
                DataParamCol[] parms = new DataParamCol[2];
                commandtexts[0] = "DELETE MXHLY_SYS_ROLE_EMP WHERE ROLE_CODE = :code ";
                parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("code", paramValue) });
                commandtexts[1] = "DELETE MXHLY_SYS_ROLE WHERE ROLE_CODE = :code";
                parms[1] = new DataParamCol(new DataParam[] { new InputDataParam("code", paramValue) });
                switch (SystemInfo.DbData.RunSql(commandtexts, parms))
                {
                    case 0:
                        base.dtList.Rows.Remove(focusedDataRow);
                        base.gvList.Focus();
                        this.freshDisp();
                        return;

                    case 1:
                        XtraMessageBox.Show("角色人员删除时不成功!\n" + SystemInfo.DbData.ErrorMessage);
                        return;
                }
                XtraMessageBox.Show("角色删除时不成功!\n" + SystemInfo.DbData.ErrorMessage);
            }
        }

        protected override void freshDisp()
        {
            DataRow focusedDataRow = this.gvRole.GetFocusedDataRow();
            if (focusedDataRow != null)
            {
                this.txtCode.Text = focusedDataRow["ROLE_CODE"].ToString();
                this.txtName.Text = focusedDataRow["ROLE_NAME"].ToString();
                this.meDescribe.Text = focusedDataRow["DESCRIBE"].ToString();
                string commandtext = "SELECT E.EMP_ID ,E.EMP_CODE ,E.EMP_NAME ,R.GRANT_FLAG ,R.GRANT_EMP_ID \r\nFROM MXHLY_SYS_ROLE_EMP R ,MXH_EMPLOYEE E \r\nWHERE R.EMP_ID = E.EMP_ID and R.ROLE_CODE = :rolecode \r\nORDER BY E.EMP_CODE ASC ";
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("rolecode", this.txtCode.Text) });
                base.dtItem.Rows.Clear();
                SystemInfo.DbData.Retrieve(commandtext, parm, ref this.dtItem);
                base.dtItem.PrimaryKey = new DataColumn[] { base.dtItem.Columns["EMP_ID"] };
            }
        }

        protected override void getChoiceItem()
        {
            int[] numArray = new int[5];
            numArray[1] = 30;
            numArray[2] = 100;
            new FrmMulSelFromGrid(base.dtItemAll, new string[] { "标识", "代码", "姓名", "扩散权", "赋权人" }, numArray, "人员选择", ref this.dtItem).ShowDialog(this);
        }

        protected override void getItemAll()
        {
            base.dtItemAll.Clear();
            SystemInfo.DbData.Retrieve("SELECT EMP_ID ,EMP_CODE ,EMP_NAME ,'N' GRANT_FLAG ,0 GRANT_EMP_ID FROM MXH_EMPLOYEE WHERE USED_FLAG = 'Y' ORDER BY EMP_CODE", ref this.dtItemAll);
            base.dtItemAll.PrimaryKey = new DataColumn[] { base.dtItemAll.Columns["EMP_ID"] };
        }

        public override bool Init()
        {
            bool flag = false;
            if (!base.Init())
            {
                return flag;
            }
            base.dtList = new DataTable();
            if (SystemInfo.OperIsAdmin)
            {
                SystemInfo.DbData.Retrieve("SELECT ROLE_CODE ,ROLE_NAME ,DESCRIBE FROM MXHLY_SYS_ROLE ORDER BY ROLE_CODE", ref this.dtList);
            }
            else
            {
                string commandtext = "SELECT R.ROLE_CODE ,R.ROLE_NAME ,R.DESCRIBE \r\n   FROM MXHLY_SYS_ROLE R ,MXHLY_SYS_ROLE_EMP E \r\n   WHERE R.ROLE_CODE = E.ROLE_CODE AND E.GRANT_FLAG = 'Y' AND E.EMP_ID = :id \r\n   ORDER BY R.ROLE_CODE ";
                SystemInfo.DbData.Retrieve(commandtext, new DataParamCol(new DataParam[] { new InputDataParam("id", SystemInfo.OperId) }), ref this.dtList);
                base.nbiNew.Visible = false;
                base.nbiDelete.Visible = false;
            }
            base.dtList.TableName = "MXHLY_SYS_ROLE";
            base.nbiChoice.Caption = "人员";
            base.InitData();
            return true;
        }
        protected override bool saveRecord()
        {
            string[] strArray;
            DataParamCol[] colArray;
            string text = this.txtCode.Text;
            string paramValue = this.txtName.Text;
            string str3 = this.meDescribe.Text;
            int index = 1;
            if (base.isNew)
            {
                strArray = new string[base.dtItem.Rows.Count + 1];
                colArray = new DataParamCol[base.dtItem.Rows.Count + 1];
                strArray[0] = "INSERT INTO MXHLY_SYS_ROLE (ROLE_CODE ,ROLE_NAME ,DESCRIBE) VALUES (:code ,:name ,:describe)";
                colArray[0] = new DataParamCol(new DataParam[] { new InputDataParam("code", text), new InputDataParam("name", paramValue), new InputDataParam("describe", str3) });
            }
            else
            {
                strArray = new string[base.dtItem.Rows.Count + 2];
                colArray = new DataParamCol[base.dtItem.Rows.Count + 2];
                string str4 = this.gvRole.GetFocusedDataRow()["ROLE_CODE"].ToString();
                strArray[0] = "DELETE MXHLY_SYS_ROLE_EMP WHERE ROLE_CODE = :code";
                colArray[0] = new DataParamCol(new DataParam[] { new InputDataParam("code", str4) });
                strArray[1] = "UPDATE MXHLY_SYS_ROLE SET ROLE_CODE = :code ,ROLE_NAME = :name ,DESCRIBE = :describe WHERE ROLE_CODE = :oldcode";
                colArray[1] = new DataParamCol(new DataParam[] { new InputDataParam("code", text), new InputDataParam("name", paramValue), new InputDataParam("describe", str3), new InputDataParam("oldcode", str4) });
                index = 2;
            }
            int num2 = 0;
            while (num2 < base.dtItem.Rows.Count)
            {
                if (base.dtItem.Rows[num2]["GRANT_EMP_ID"].ToString() == "0")
                {
                    base.dtItem.Rows[num2]["GRANT_EMP_ID"] = SystemInfo.OperId;
                }
                strArray[index] = "INSERT INTO MXHLY_SYS_ROLE_EMP (ROLE_CODE ,EMP_ID ,GRANT_FLAG ,GRANT_EMP_ID) VALUES (:role_code ,:emp_id ,:grant_flag ,:grant_emp_id)";
                colArray[index] = new DataParamCol(new DataParam[] { new InputDataParam("role_code", text), new InputDataParam("emp_id", base.dtItem.Rows[num2]["EMP_ID"]), new InputDataParam("grant_flag", base.dtItem.Rows[num2]["GRANT_FLAG"]), new InputDataParam("grant_emp_id", base.dtItem.Rows[num2]["GRANT_EMP_ID"]) });
                num2++;
                index++;
            }
            if (SystemInfo.DbData.RunSql(strArray, colArray) == 0)
            {
                if (base.isNew)
                {
                    DataRow row = base.dtList.NewRow();
                    row["ROLE_CODE"] = text;
                    row["ROLE_NAME"] = paramValue;
                    row["DESCRIBE"] = str3;
                    base.dtList.Rows.Add(row);
                }
                else
                {
                    DataRow focusedDataRow = this.gvRole.GetFocusedDataRow();
                    focusedDataRow["ROLE_CODE"] = text;
                    focusedDataRow["ROLE_NAME"] = paramValue;
                    focusedDataRow["DESCRIBE"] = str3;
                }
                return true;
            }
            XtraMessageBox.Show("保存数据不成功！\n原因：" + SystemInfo.DbData.ErrorMessage, "出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            return false;
        }
    }
}
